/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Util;

import com.webaccess.retailws.Bean.MailBean;
import com.webaccess.retailws.Bean.NewsletterBean;
import com.webaccess.retailws.Delegate.MailDelegate;
import com.webaccess.retailws.Delegate.MenuDelegate;

/**
 *
 * @author Owner
 */
public class menuUtil {

    MenuDelegate menuDelegateObj = null;
    NewsletterBean newslBean = null;
    MailBean mailInfo = null;
    SendMailUtil mailUtil = null;
    CommonUtil commUtil = null;
    MailDelegate mailDelegateObj = null;

    public String subscribeNL(Long newsLetterId, String newsletterEmail) {
        menuDelegateObj = new MenuDelegate();
        newslBean = new NewsletterBean();
        commUtil = new CommonUtil();
        mailDelegateObj = new MailDelegate();
        mailUtil = new SendMailUtil();
        String SUCCESS = "homepageshow";
        try {
            newslBean.setNewsLetterId(newsLetterId);
            newslBean.setNewsLetterEmail(newsletterEmail);
            boolean isNewsLetterSaved = menuDelegateObj.subscribeNL(newslBean);
            if (newsLetterId > 0) {
                if (isNewsLetterSaved == true) {
                    StringBuffer messageBody = new StringBuffer();
                    messageBody.append("E Shop Newsletter UnSubscription");
                    messageBody.append("\n\n");
                    messageBody.append("Hi,");
                    messageBody.append("\n\n");
                    messageBody.append("You have successfully unsubcribed from Eshop Newsletters ");
                    messageBody.append("\n\n");
                    mailInfo = new MailBean();
                    mailInfo.setFromAddress("abhishek.agrawal@webaccess.co.in");
                    mailInfo.setToAddress(newsletterEmail);
                    mailInfo.setMsgSubject("E Shop - Newsletter Subscription");
                    mailInfo.setMsgContent(messageBody.toString());
                    mailInfo.setMsgDateTime(commUtil.getCurrentDateTime());
                    mailInfo.setUserId(0L);
                    mailInfo.setPurpose("Newsletter UnSubcription");

                    Long mailId = mailDelegateObj.saveMailInfo(mailInfo);
                    if (mailId > 0) {
                        mailUtil.sendMail(mailInfo.getFromAddress(), mailInfo.getToAddress(), mailInfo.getMsgSubject(), mailInfo.getMsgContent());
                    }
                }
            } else {
                if (isNewsLetterSaved == true) {
                    StringBuffer messageBody = new StringBuffer();
                    messageBody.append("E Shop Newsletter Subscription");
                    messageBody.append("\n\n");
                    messageBody.append("Hi,");
                    messageBody.append("\n\n");
                    messageBody.append("Thanks for subscribing to the EShop Newsletter. You will now receive updates on the latest products.");
                    messageBody.append("\n\n");
                    messageBody.append("Happy Shopping! ");
                    messageBody.append("\n");
                    mailInfo = new MailBean();
                    mailInfo.setFromAddress("abhishek.agrawal@webaccess.co.in");
                    mailInfo.setToAddress(newsletterEmail);
                    mailInfo.setMsgSubject("E Shop - Newsletter Subscription");
                    mailInfo.setMsgContent(messageBody.toString());
                    mailInfo.setMsgDateTime(commUtil.getCurrentDateTime());
                    mailInfo.setUserId(0L);
                    mailInfo.setPurpose("Newsletter Subscription");

                    Long mailId = mailDelegateObj.saveMailInfo(mailInfo);
                    if (mailId > 0) {
                        mailUtil.sendMail(mailInfo.getFromAddress(), mailInfo.getToAddress(), mailInfo.getMsgSubject(), mailInfo.getMsgContent());
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return SUCCESS;
        }


    }
}
